package com.control.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public final class SocketUtils {
	
	private static String fileName = "config/sys.properties";
	
	private static ServerSocket ss = null;
	
	public static final int SERVER_PORT = Integer.parseInt(PropertyUtils.get(fileName, "SERVER_PORT"));
	
	public static ServerSocket getInstance() throws IOException{
		if(ss == null || ss.isClosed()){
			ss = new ServerSocket(SERVER_PORT);
		}
		return ss;
	}
	
	public static void close(PrintWriter pw,BufferedReader br,Socket s,ServerSocket ss){
		try {
			
			if(pw!=null){ 
				pw.flush();
				pw.close();
				pw = null;
			}
			if(br!=null){
				br.close();
				br = null;
			}
			if(s!=null){
				s.close();
				s = null;
			}
			if(ss!=null){
				ss.close();
				ss = null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
